<template>
<!--  <doc-alert title="会员用户、标签、分组" url="https://doc.oa.syzl.net.cn/member/user/" />-->

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <el-form-item label="项目编号" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          class="!w-240px"
          clearable
          placeholder="请输入项目编号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          class="!w-240px"
          clearable
          placeholder="请输入项目名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item> 
      <el-form-item label="项目类别" prop="types">
        
        <el-select v-model="queryParams.typeId" placeholder="请选择项目类别" clearable   class="!w-240px">
          <el-option v-for="type in types" :key="type.id" :label="type.name" :value="type.id" />
        </el-select> 
      </el-form-item> 
   
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button> 
        <el-button   type="primary" @click="openForm">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :show-overflow-tooltip="true"
      :stripe="true"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" label="项目编号" prop="no" width="120px" /> 
      <el-table-column align="center" label="项目名称" prop="name"  /> 
      <el-table-column align="center" label="项目类型" prop="type"   />  
      <el-table-column align="center" label="项目经理" prop="pm"  />  
      <el-table-column align="center" label="客户经理" prop="kehu_pm"  />   
        <el-table-column align="center" label="项目状态" prop="status" width="100px">
        <template #default="scope"> 
          <el-tag size="small">{{scope.row.status}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目预算" prop="yusuan_money"  :formatter="moneyFomatter"  />  
      <el-table-column align="center" label="启动时间"  prop="qidong_time"  width="180px"  :formatter="dateFormatter"/> 
      <el-table-column align="center" label="交付时间"  prop="jiaofu_time"  width="180px"  :formatter="dateFormatter"/>  
     
      <el-table-column
        :show-overflow-tooltip="false"
        align="center"
        fixed="right"
        label="操作"
        width="100px"
      >
        <template #default="scope">
          <div class="flex items-center justify-center">
            <el-button link type="primary" @click="openDetail(scope.row.id)">详情</el-button> 
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
  
  <!-- 发送优惠券弹窗 -->
  <CouponSendForm ref="couponSendFormRef" />
  <!-- 添加或修改用户对话框 -->
  <projectForm ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime' 

import * as UserApi from '@/api/member/user'
import { DICT_TYPE } from '@/utils/dict' 
import MemberTagSelect from '@/views/member/tag/components/MemberTagSelect.vue'
import MemberLevelSelect from '@/views/member/level/components/MemberLevelSelect.vue'
import MemberGroupSelect from '@/views/member/group/components/MemberGroupSelect.vue'
 
import { CouponSendForm } from '@/views/mall/promotion/coupon/components'
import { checkPermi } from '@/utils/permission'
import projectForm from './projectForm.vue'

defineOptions({ name: 'MemberUser' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickname: null,
  mobile: null,
  loginDate: [],
  createTime: [],
  tagIds: [],
  levelId: null,
  groupId: null
})
const queryFormRef = ref() // 搜索的表单
const updateLevelFormRef = ref() // 修改会员等级表单
const updatePointFormRef = ref() // 修改会员积分表单
const UpdateBalanceFormRef = ref() // 修改用户余额表单
const selectedIds = ref<number[]>([]) // 表格的选中 ID 数组


const types = reactive([
  {id:"1",name:"授予类"},{id:"2",name:"竞标类"},  {id:"3",name:"自采类"},{id:"4",name:"海外类"},{id:"5",name:"咨询类"}
]);


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    list.value = [{id:88,no:'202411-A01',name:'张三',type:'授予类需求',pm:'数虎张三',kehu_pm:'客户张三',status:'前期',yusuan_money:1000000000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:2,no:'202411-A02',name:'张三',type:'授予类需求',pm:'数虎张三1',kehu_pm:'客户张三1',status:'待PO',yusuan_money:2000000000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:3,no:'202411-A03',name:'张三',type:'授予类需求',pm:'数虎张三2',kehu_pm:'客户张三2',status:'开工执行',yusuan_money:130000000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:4,no:'202411-A04',name:'张三',type:'授予类需求',pm:'数虎张三3',kehu_pm:'客户张三3',status:'前期',yusuan_money:14000000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:5,no:'202411-A05',name:'张三',type:'授予类需求',pm:'数虎张三4',kehu_pm:'客户张三4',status:'待验收',yusuan_money:600000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:6,no:'202411-A06',name:'张三',type:'授予类需求',pm:'数虎张三5',kehu_pm:'客户张三5',status:'前期',yusuan_money:880000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:7,no:'202411-A07',name:'张三',type:'授予类需求',pm:'数虎张三6',kehu_pm:'客户张三6',status:'前期',yusuan_money:99900,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:8,no:'202411-A08',name:'张三',type:'授予类需求',pm:'数虎张三7',kehu_pm:'客户张三7',status:'前期',yusuan_money:178400000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:9,no:'202411-A09',name:'张三',type:'授予类需求',pm:'数虎张三8',kehu_pm:'客户张三8',status:'前期',yusuan_money:176000000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},
{id:10,no:'202411-A010',name:'张三',type:'授予类需求',pm:'数虎张三9',kehu_pm:'客户张三9',status:'前期',yusuan_money:2000000,qidong_time:'2024-11-14 10:00:00',jiaofu_time:'2025-11-14 10:00:00'},]
    total.value = 10





  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  //queryParams.pageNo = 1
  //getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 打开会员详情 */
const { push } = useRouter()
const openDetail = (id: number) => {
  push({ name: 'ProjectDetail', params: { id } })
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 表格选中事件 */
const handleSelectionChange = (rows: UserApi.UserVO[]) => {
  selectedIds.value = rows.map((row) => row.id)
}

/** 发送优惠券 */
const couponSendFormRef = ref()
const openCoupon = () => {
  if (selectedIds.value.length === 0) {
    message.warning('请选择要发送优惠券的用户')
    return
  }
  couponSendFormRef.value.open(selectedIds.value)
}

/** 操作分发 */
const handleCommand = (command: string, row: UserApi.UserVO) => {
  switch (command) {
    case 'handleUpdate':
      openForm('update', row.id)
      break
    case 'handleUpdateLevel':
      updateLevelFormRef.value.open(row.id)
      break
    case 'handleUpdatePoint':
      updatePointFormRef.value.open(row.id)
      break
    case 'handleUpdateBlance':
      UpdateBalanceFormRef.value.open(row.id)
      break
    default:
      break
  }
}

const moneyFomatter = (row, column, cellValue)=> {
   cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
   if (!cellValue.includes('.')) cellValue += '.';
    return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
}



/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
